﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WorldDomination.GoogleApi.AjaxSearch;


namespace WorldDomination.GoogleApi.Tests
{
    [TestClass]
    public class AjaxSearchTest
    {
        private static void AssertCheck(Search search)
        {
            Assert.IsNotNull(search);
            Assert.IsNotNull(search.ResponseData);
            Assert.IsNotNull(search.ResponseData.Results);
            Assert.IsTrue(search.ResponseData.Results.Length > 0);
        }

        [TestMethod]
        public void SearchMoreThanOneResultFoundTest()
        {
            Search search;
            const string referer = "http://www.MySite.com";


            search = Search.GetSearchResults("star wars",
                referer,
                300);
            AjaxSearchTest.AssertCheck(search);

            search = Search.GetSearchResults("asp.net",
                referer);
            AjaxSearchTest.AssertCheck(search);

            search = Search.GetSearchResults("wicked weasel bikini's",
                referer);
            AjaxSearchTest.AssertCheck(search);

            search = Search.GetSearchResults("model",
                referer,
                "wickedweasel.com",
                0,
                null);
            AjaxSearchTest.AssertCheck(search);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void SearchKeywordExceptionTest()
        {
            Search.GetSearchResults(null,
                null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void SearchRefererExceptionTest()
        {
            Search.GetSearchResults("blah",
                null);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void SearchInvalidRefererExceptionTest()
        {
            Search.GetSearchResults("blah",
                "bad.url.com");
        }
    }
}